package com.hust.hui.quick.service.dao;

import com.hust.hui.quick.service.domain.ArticleEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by yihui on 2017/7/19.
 */
//@Mapper
@Repository
public interface ArticleMapper {


    @Update("CREATE TABLE `articles` (\n" +
            "  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n" +
            "  `userId` int(11) NOT NULL DEFAULT '0',\n" +
            "  `title` varchar(40) NOT NULL DEFAULT '',\n" +
            "  `desc` varchar(500) NOT NULL DEFAULT '0',\n" +
            "  `content` text NOT NULL,\n" +
            "  `dirId` int(11) NOT NULL DEFAULT '0',\n" +
            "  `img` varchar(80) NOT NULL DEFAULT '' COMMENT '主图',\n" +
            "  `link` varchar(80) NOT NULL DEFAULT '' COMMENT '源博客链接',\n" +
            "  `tags` varchar(50) NOT NULL DEFAULT '' COMMENT '标签,英文逗号分割',\n" +
            "  `state` tinyint(4) NOT NULL DEFAULT '0' COMMENT '0 草稿  1 已发表',\n" +
            "  `sort` bigint(14) NOT NULL DEFAULT '0' COMMENT '非置顶，为发布时间；否则为发布时间最左补1',\n" +
            "  `isDeleted` tinyint(1) NOT NULL DEFAULT '0',\n" +
            "  `publishTime` int(13) NOT NULL DEFAULT '0',\n" +
            "  `created` int(13) NOT NULL DEFAULT '0',\n" +
            "  `updated` int(13) NOT NULL DEFAULT '0',\n" +
            "  PRIMARY KEY (`id`),\n" +
            "  KEY `idx_userId_sort` (`userId`,`sort`),\n" +
            "  KEY `idx_title_sort` (`title`,`sort`),\n" +
            "  KEY `idx_dirId_sort` (`dirId`,`sort`)\n" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
    void create();


    @Insert("insert into articles (`userId`, `title`, `desc`, `content`, `dirId`, `img`, `link`, `tags`, `state`, `sort`, `isDeleted`,`publishTime`, `created`, `updated`)" +
            " values (" +
            "#{userId}, #{title}, #{desc}, #{content}, #{dirId}, #{img}, #{link}," +
            "#{tags}, #{state}, #{sort}, 0, #{publishTime}, unix_timestamp(now()), unix_timestamp(now())" +
            ");")
    int insert(ArticleEntity articleEntity);


    /**
     * 查询用户的所有博客内容；
     * 查询目录下的所有博客内容
     * 根据id查询 （包括批量）
     * 根据标题进行模糊查询
     *
     * @param map
     */
    @Select("<script>" +
            " select * from articles " +
            "<where>" +
            "   <if test='user!=null'> userId=#{userId} </if>" +
            "   <if test='dirId!=null'> and dirId=#{dirId} </if>" +
            "   <if test='dirIds!=null'> and dirId in " +
            "       <foreach collection='dirIds' index='index' item='dirId' open='(' separator=',' close=')'>" +
            "               #{dirId}" +
            "       </foreach> \" +" +
            "   </if>" +
            "   <if test='id!=null'> and id=#{id} </if>" +
            "   <if test='ids!=null'> and id in " +
            "       <foreach collection='ids' index='index' item='id' open='(' separator=',' close=')'>" +
            "           #{id}" +
            "       </foreach>" +
            "   </if>" +
            "   <if test='title!=null'>" +
            "       and title LIKE CONCAT('%',#{title},'%')" +
            "   </if>" +
            "</where>" +
            " order by sort desc" +
            " limit 1000" +
            "</script>")
    List<ArticleEntity> query(Map<String, Object> map);


    @Select("select * from Articles where id=#{id} limit 1")
    ArticleEntity queryById(@Param("id") long id);


    @Update("<script>" +
            "update articles " +
            "<set>" +
            "   <if test='title!=null'> title = #{title},</if>" +
            "   <if test='desc!=null'> `desc` = #{desc}, </if>" +
            "   <if test='content!=null'> content = #{content}, </if>" +
            "   <if test='link!=null'>link = #{link}, </if>" +
            "   <if test='tags!=null'> tags = #{tags}, </if>" +
            "   <if test='dirId>0'> dirId = #{dirId}, </if>" +
            "   <if test='state>-1'> state = #{state}, </if>" + // 发不到草稿 or 上架
            "   <if test='sort>0'> sort = #{sort}, </if>" +  // 是否置顶
            "   <if test='isDeleted>-1'> isDeleted = #{isDeleted}, </if>" +
            "   <if test='publishTime>0'> publishTime = #{publishTime}, </if>" +
            "   updated = unix_timestamp(now())" +
            "</set>" +
            "<where>" +
            "   id = #{id}" +
            "</where>" +
            "</script>")
    int update(ArticleEntity articleEntity);
}
